package com.xmy.cultivate.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.xmy.cultivate.entity.Quarter;
import com.xmy.cultivate.entity.SchoolQuarter;
import com.xmy.cultivate.mapper.AppointSchoolMapper;
import com.xmy.cultivate.respons.AiSchoolSite;
import com.xmy.cultivate.service.IAiSchoolSiteService;
import com.xmy.cultivate.service.IQuarterService;
import com.xmy.cultivate.service.ISchoolQuarterService;
import com.xmy.cultivate.util.Code;
import com.xmy.cultivate.util.PageResult;
import com.xmy.cultivate.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 校区场地 前端控制器
 * </p>
 *
 * @author hpiggy
 * @since 2024-10-28
 */
@RestController
@RequestMapping("/api/aiSchoolSite")
public class AiSchoolSiteController extends BaseController {
    @Autowired
    IAiSchoolSiteService iAiSchoolSiteService;

    @Autowired
    IQuarterService iQuarterService;

    @Autowired
    ISchoolQuarterService iSchoolQuarterService;

    @Autowired
    @Lazy
    AppointSchoolMapper appointSchoolMapper;

    /**
     * 获取场地
     */
    @GetMapping("")
    public Result getAiSchoolSiteList(@RequestParam(value = "yearPart", required = false) String yearPart,
                                      @RequestParam(value = "quarterNum", required = false) String quarterNum,
                                      @RequestParam(value = "schoolId", required = false) String schoolId) {

        if (yearPart == null || quarterNum == null) {
            Quarter eduQuarter = iQuarterService.getNowQuarter();
            if (eduQuarter == null) {
                return null;
            }
            yearPart = eduQuarter.getYearPart().toString();
            quarterNum = eduQuarter.getNum().toString();
        }

        // 判断是否为超级管理员
        if (!this.adminId.equals(1568138350324350975L)) {
            // 获取有权限的校区
            List<Long> schoolIdList = appointSchoolMapper.getSchoolIdListForForId(this.adminId, 2);
            // 根据校区查询出大校区
            if (schoolIdList != null && schoolIdList.size() > 0) {
                StringBuffer schoolIdStr = new StringBuffer();
                for (Long schoolIdItem : schoolIdList) {
                    schoolIdStr.append(schoolIdItem).append(",");
                }
                schoolId = schoolIdStr.substring(0, schoolIdStr.length() - 1);
                schoolId = iSchoolQuarterService.getBigSchoolIdStr(yearPart, quarterNum, schoolId);
            }
        }

        // 获取大校区接口
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("year_part", yearPart);
        queryWrapper.eq("quarter_num", quarterNum);
        queryWrapper.inSql(StringUtils.isNotBlank(schoolId), "school_id", schoolId);
        queryWrapper.eq("pid", 0);
        queryWrapper.eq("deleted", 0);
        List<SchoolQuarter> schoolQuarterList = iSchoolQuarterService.list(queryWrapper);

        for (SchoolQuarter schoolQuarter : schoolQuarterList) {
            // 获取小场地
            QueryWrapper wrapper = new QueryWrapper<>();
            wrapper.eq("year_part", yearPart);
            wrapper.eq("quarter_num", quarterNum);
            wrapper.eq("school_id", schoolQuarter.getSchoolId());
            wrapper.eq("status", 1);
            List<AiSchoolSite> schoolSites = iAiSchoolSiteService.list(wrapper);
            schoolQuarter.setChildRateList(schoolSites);
        }

        //List<SchoolQuarter> aiSchoolSiteList = iAiSchoolSiteService.aiSchoolSiteList(yearPart, quarterNum, schoolId);

        return new Result(Code.OK, schoolQuarterList, Code.GET_OK_MSG);
    }




}
